<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li class="active"><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. React测试初体验">1. React测试初体验</a></li><li><a href="#t12.enzyme测试">2.enzyme测试</a></li><li><a href="#t23.点击事件">3.点击事件</a></li><li><a href="#t34. TDD">4. TDD</a></li><li><a href="#t45. 测试点击事件">5. 测试点击事件</a></li><li><a href="#t56. mount">6. mount</a></li><li><a href="#t6参考">参考</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. React&#x6D4B;&#x8BD5;&#x521D;&#x4F53;&#x9A8C;">1. React&#x6D4B;&#x8BD5;&#x521D;&#x4F53;&#x9A8C; <a href="#t01. React&#x6D4B;&#x8BD5;&#x521D;&#x4F53;&#x9A8C;"> # </a></h2>
<pre><code class="lang-js">create-react-app zf-react-test
cd zf-react-test
npm test
</code></pre>
<p>sum.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">a,b</span>)</span>{
  <span class="hljs-keyword">return</span> a+b;
}
</code></pre>
<p>sum.spec.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> sum = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./sum&apos;</span>);
it(<span class="hljs-string">&apos;a+b&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    expect(sum(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)).toBe(<span class="hljs-number">3</span>);
});
</code></pre>
<h2 id="t12.enzyme&#x6D4B;&#x8BD5;">2.enzyme&#x6D4B;&#x8BD5; <a href="#t12.enzyme&#x6D4B;&#x8BD5;"> # </a></h2>
<p><img src="http://img.zhufengpeixun.cn/enzyme.jpg.png" alt="enzyme"></p>
<ul>
<li>Shallow Rendering(&#x6D45;&#x6E32;&#x67D3;)&#x6307;&#x7684;&#x662F;&#xFF0C;&#x5C06;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x6210;&#x865A;&#x62DF;DOM&#x5BF9;&#x8C61;&#xFF0C;&#x4F46;&#x662F;&#x53EA;&#x6E32;&#x67D3;&#x7B2C;&#x4E00;&#x5C42;&#xFF0C;&#x4E0D;&#x6E32;&#x67D3;&#x6240;&#x6709;&#x5B50;&#x7EC4;&#x4EF6;&#xFF0C;&#x6240;&#x4EE5;&#x5904;&#x7406;&#x901F;&#x5EA6;&#x975E;&#x5E38;&#x5FEB;&#x3002;&#x5B83;&#x4E0D;&#x9700;&#x8981;DOM&#x73AF;&#x5883;&#xFF0C;&#x56E0;&#x4E3A;&#x6839;&#x672C;&#x6CA1;&#x6709;&#x52A0;&#x8F7D;&#x8FDB;DOM</li>
<li>find()&#x65B9;&#x6CD5;&#xFF1A;&#x53EA;&#x652F;&#x6301;&#x7B80;&#x5355;&#x9009;&#x62E9;&#x5668; &#x7C7B;&#x9009;&#x62E9;&#x5668;&#x3001;id&#x9009;&#x62E9;&#x5668;&#x3001;&#x6807;&#x7B7E;&#x9009;&#x62E9;&#x5668;&#x3001;&#x590D;&#x5408;&#x9009;&#x62E9;&#x5668;</li>
</ul>
<pre><code class="lang-js">cnpm i enzyme enzyme-adapter-react<span class="hljs-number">-16</span> -D
</code></pre>
<p>TodoInput.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoInput</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>&#x5F85;&#x529E;&#x4E8B;&#x9879;<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">defaultValue</span>=<span class="hljs-string">&quot;&#x8BF7;&#x8F93;&#x5165;&quot;</span>/&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    );
  }
}

export default TodoInput;
</span></code></pre>
<p>TodoInput.spec.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Enzyme,{shallow} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme&apos;</span>;
<span class="hljs-keyword">import</span> TodoInput <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./TodoInput&apos;</span>;
<span class="hljs-keyword">import</span> Adapter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme-adapter-react-16&apos;</span>; 
Enzyme.configure({<span class="hljs-attr">adapter</span>:<span class="hljs-keyword">new</span> Adapter()});
<span class="hljs-comment">//http://airbnb.io/enzyme/docs/api/ShallowWrapper/find.html</span>
<span class="hljs-comment">//http://airbnb.io/enzyme/docs/api/selector.html</span>
it(<span class="hljs-string">&apos;shoud render &#x8BF7;&#x8F93;&#x5165;&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">const</span> wrapper  = shallow(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">TodoInput</span>/&gt;</span></span>);
    <span class="hljs-keyword">const</span> h3 = wrapper.find(<span class="hljs-string">&apos;h3&apos;</span>);
    <span class="hljs-keyword">const</span> input = wrapper.find(<span class="hljs-string">&apos;input&apos;</span>);
    expect(h3.text()).toBe(<span class="hljs-string">&apos;&#x5F85;&#x529E;&#x4E8B;&#x9879;&apos;</span>);
    expect(input.props().defaultValue).toBe(<span class="hljs-string">&apos;&#x8BF7;&#x8F93;&#x5165;&apos;</span>);
});
</code></pre>
<p>src\components\TodoList.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">static</span> propTypes = {
    <span class="hljs-attr">todos</span>:PropTypes.arrayOf(PropTypes.shape({
      <span class="hljs-attr">text</span>:PropTypes.string.isRequired,
      <span class="hljs-attr">createAt</span>:PropTypes.object.isRequired
    }))
  }
</code></pre>
<h2 id="t23.&#x70B9;&#x51FB;&#x4E8B;&#x4EF6;">3.&#x70B9;&#x51FB;&#x4E8B;&#x4EF6; <a href="#t23.&#x70B9;&#x51FB;&#x4E8B;&#x4EF6;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Enzyme,{shallow} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme&apos;</span>;
<span class="hljs-keyword">import</span> TodoInput <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./TodoInput&apos;</span>;
<span class="hljs-keyword">import</span> Adapter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme-adapter-react-16&apos;</span>; 
Enzyme.configure({<span class="hljs-attr">adapter</span>:<span class="hljs-keyword">new</span> Adapter()});
<span class="hljs-comment">//http://airbnb.io/enzyme/docs/api/ShallowWrapper/find.html</span>
<span class="hljs-comment">//http://airbnb.io/enzyme/docs/api/selector.html</span>
describe(<span class="hljs-string">&apos;&#x6D4B;&#x8BD5;TodoInput&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">let</span> todos;
    beforeEach(<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{
        todos =[{<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;1&apos;</span>},{<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;2&apos;</span>}];
    });
    it(<span class="hljs-string">&apos;shoud render &#x8BF7;&#x8F93;&#x5165;&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
        <span class="hljs-keyword">let</span> wrapper =  shallow(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">TodoInput</span>/&gt;</span></span>);
        <span class="hljs-keyword">const</span> h3 = wrapper.find(<span class="hljs-string">&apos;h3&apos;</span>);
        <span class="hljs-keyword">const</span> input = wrapper.find(<span class="hljs-string">&apos;input&apos;</span>);
        expect(h3.text()).toBe(<span class="hljs-string">&apos;&#x5F85;&#x529E;&#x4E8B;&#x9879;&apos;</span>);
        expect(input.props().defaultValue).toBe(<span class="hljs-string">&apos;&#x8BF7;&#x8F93;&#x5165;&apos;</span>);
    });

    it(<span class="hljs-string">&apos;&#x70B9;&#x51FB;&#x6309;&#x94AE;&#x7684;&#x65F6;&#x5019;&#x8C03;&#x7528;addTodo&#x65B9;&#x6CD5;&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
        <span class="hljs-keyword">let</span> addTodo = jest.fn();
        <span class="hljs-keyword">let</span> wrapper = shallow(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">TodoInput</span> <span class="hljs-attr">addTodo</span> = <span class="hljs-string">{addTodo}/</span>&gt;</span>);
        let button = wrapper.find(&apos;button&apos;);
        button.simulate(&apos;click&apos;);
        expect(addTodo).toBeCalled();
    });
});

</span></code></pre>
<h2 id="t34. TDD">4. TDD <a href="#t34. TDD"> # </a></h2>
<ul>
<li>TDD&#x662F;&#x6D4B;&#x8BD5;&#x9A71;&#x52A8;&#x5F00;&#x53D1;&#xFF08;Test-Driven Development&#xFF09;&#x662F;&#x654F;&#x6377;&#x5F00;&#x53D1;&#x4E2D;&#x7684;&#x4E00;&#x9879;&#x6838;&#x5FC3;&#x5B9E;&#x8DF5;&#x548C;&#x6280;&#x672F;&#xFF0C;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x8BBE;&#x8BA1;&#x65B9;&#x6CD5;&#x8BBA;</li>
<li>TDD&#x7684;&#x539F;&#x7406;&#x662F;&#x5728;&#x5F00;&#x53D1;&#x529F;&#x80FD;&#x4EE3;&#x7801;&#x4E4B;&#x524D;&#xFF0C;&#x5148;&#x7F16;&#x5199;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x4EE3;&#x7801;&#xFF0C;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x786E;&#x5B9A;&#x9700;&#x8981;&#x7F16;&#x5199;&#x4EC0;&#x4E48;&#x4EA7;&#x54C1;&#x4EE3;&#x7801;</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> reducer = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./reducer&apos;</span>);
<span class="hljs-keyword">const</span> ADD_TODO = <span class="hljs-string">&apos;ADD_TODO&apos;</span>;
<span class="hljs-keyword">const</span> DEL_TODO = <span class="hljs-string">&apos;DEL_TODO&apos;</span>;
describe(<span class="hljs-string">&apos;reducer&apos;</span>,()=&gt;{
    <span class="hljs-keyword">let</span> INIT_STATE = [{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;1&apos;</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;2&apos;</span>}];

    it(<span class="hljs-string">&apos;&#x521D;&#x59CB;&#x72B6;&#x6001;&apos;</span>,()=&gt;{
        expect(reducer(<span class="hljs-literal">undefined</span>,{})).toEqual(INIT_STATE);
    });

    it(<span class="hljs-string">&apos;&#x589E;&#x52A0;todo&apos;</span>,()=&gt;{
        <span class="hljs-keyword">let</span> todos = reducer(INIT_STATE,{<span class="hljs-attr">type</span>:ADD_TODO,<span class="hljs-attr">todo</span>:{<span class="hljs-attr">id</span>:<span class="hljs-number">3</span>,<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;3&apos;</span>}});
        expect(todos).toEqual([...INIT_STATE,{<span class="hljs-attr">id</span>:<span class="hljs-number">3</span>,<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;3&apos;</span>}]);
    });
    it(<span class="hljs-string">&apos;&#x5220;&#x9664;todo&apos;</span>,()=&gt;{
        <span class="hljs-keyword">let</span> todos = reducer(INIT_STATE,{<span class="hljs-attr">type</span>:DEL_TODO,<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>});
        expect(todos).toEqual([{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;1&apos;</span>}]);
    });
});
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> ADD_TODO = <span class="hljs-string">&apos;ADD_TODO&apos;</span>;
<span class="hljs-keyword">const</span> DEL_TODO = <span class="hljs-string">&apos;DEL_TODO&apos;</span>;
<span class="hljs-keyword">let</span> INIT_STATE = [{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;1&apos;</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">text</span>:<span class="hljs-string">&apos;2&apos;</span>}];
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reducer</span>(<span class="hljs-params">state=INIT_STATE,action={}</span>)</span>{
  <span class="hljs-keyword">switch</span>(action.type){
      <span class="hljs-keyword">case</span> ADD_TODO:
          <span class="hljs-keyword">return</span> [...state,action.todo];
      <span class="hljs-keyword">case</span> DEL_TODO:
          <span class="hljs-keyword">return</span> state.filter(<span class="hljs-function"><span class="hljs-params">item</span>=&gt;</span>item.id != action.id);
      <span class="hljs-keyword">default</span>:
          <span class="hljs-keyword">return</span> state;    
  }
}
<span class="hljs-built_in">module</span>.exports = reducer;
</code></pre>
<h2 id="t45. &#x6D4B;&#x8BD5;&#x70B9;&#x51FB;&#x4E8B;&#x4EF6;">5. &#x6D4B;&#x8BD5;&#x70B9;&#x51FB;&#x4E8B;&#x4EF6; <a href="#t45. &#x6D4B;&#x8BD5;&#x70B9;&#x51FB;&#x4E8B;&#x4EF6;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Enzyme,{shallow} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme&apos;</span>;
<span class="hljs-keyword">import</span> TodoItem <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./TodoItem&apos;</span>;
<span class="hljs-keyword">import</span> Adapter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme-adapter-react-16&apos;</span>; 
<span class="hljs-keyword">import</span> { wrap } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;module&apos;</span>;
Enzyme.configure({<span class="hljs-attr">adapter</span>:<span class="hljs-keyword">new</span> Adapter()});
<span class="hljs-comment">//http://airbnb.io/enzyme/docs/api/ShallowWrapper/find.html</span>
<span class="hljs-comment">//http://airbnb.io/enzyme/docs/api/selector.html</span>
describe(<span class="hljs-string">&apos;TodoItem&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    it(<span class="hljs-string">&apos;todo&apos;</span>,()=&gt;{
        <span class="hljs-keyword">const</span> wrapper = shallow(&lt;TodoItem todo={{id:1,text:&apos;1&apos;}}/&gt;);
        expect(wrapper.text()).toMatch(/1/);
        expect(wrapper.hasClass(&apos;todo&apos;)).toBe(true);
        expect(wrapper.hasClass(&apos;todo-selected&apos;)).toBe(false);
    });
    it(&apos;todo-selected&apos;,()=&gt;{
        const wrapper = shallow(&lt;TodoItem todo={{id:1,text:&apos;1&apos;}}/&gt;);
        expect(wrapper.text()).toMatch(/1/);
        wrapper.simulate(&apos;click&apos;);
        expect(wrapper.hasClass(&apos;todo&apos;)).toBe(false);
        expect(wrapper.hasClass(&apos;todo-selected&apos;)).toBe(true);
    });
});

</code></pre>
<h2 id="t56. mount">6. mount <a href="#t56. mount"> # </a></h2>
<p>mount&#x5C06;React&#x7EC4;&#x4EF6;&#x52A0;&#x8F7D;&#x4E3A;&#x771F;&#x5B9E;&#x7684;DOM</p>
<ul>
<li>.get(index)&#xFF1A;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x7684;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;DOM&#x8282;&#x70B9;</li>
<li>.at(index)&#xFF1A;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x7684;&#x5B50;&#x7EC4;&#x4EF6;</li>
<li>.text()&#xFF1A;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x7EC4;&#x4EF6;&#x7684;&#x6587;&#x672C;&#x5185;&#x5BB9;</li>
<li>.html()&#xFF1A;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x7EC4;&#x4EF6;&#x7684;HTML&#x4EE3;&#x7801;&#x5F62;&#x5F0F;</li>
<li>.props()&#xFF1A;&#x8FD4;&#x56DE;&#x6839;&#x7EC4;&#x4EF6;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;</li>
<li>.prop(key)&#xFF1A;&#x8FD4;&#x56DE;&#x6839;&#x7EC4;&#x4EF6;&#x7684;&#x6307;&#x5B9A;&#x5C5E;&#x6027;</li>
<li>.state([key])&#xFF1A;&#x8FD4;&#x56DE;&#x6839;&#x7EC4;&#x4EF6;&#x7684;&#x72B6;&#x6001;</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> Enzyme,{mount} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme&apos;</span>;
<span class="hljs-keyword">import</span> TodoApp <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./TodoApp&apos;</span>;
<span class="hljs-keyword">import</span> Adapter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;enzyme-adapter-react-16&apos;</span>; 
Enzyme.configure({<span class="hljs-attr">adapter</span>:<span class="hljs-keyword">new</span> Adapter()});
describe(<span class="hljs-string">&apos;TodoApp&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
   it(<span class="hljs-string">&apos;addTodo&apos;</span>,()=&gt;{
    <span class="hljs-keyword">let</span> wrapper = mount(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">TodoApp</span>/&gt;</span></span>);
    <span class="hljs-keyword">let</span> len = wrapper.find(<span class="hljs-string">&apos;li&apos;</span>).length;
    wrapper.find(<span class="hljs-string">&apos;button&apos;</span>).at(<span class="hljs-number">0</span>).simulate(<span class="hljs-string">&apos;click&apos;</span>);
    expect(wrapper.find(<span class="hljs-string">&apos;li&apos;</span>).length).toBe(len + <span class="hljs-number">1</span>);
   });
   it(<span class="hljs-string">&apos;delTodo&apos;</span>,()=&gt;{
    <span class="hljs-keyword">let</span> wrapper = mount(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">TodoApp</span>/&gt;</span></span>);
    <span class="hljs-keyword">let</span> len = wrapper.find(<span class="hljs-string">&apos;li&apos;</span>).length;
    wrapper.find(<span class="hljs-string">&apos;button&apos;</span>).at(<span class="hljs-number">1</span>).simulate(<span class="hljs-string">&apos;click&apos;</span>);
    expect(wrapper.find(<span class="hljs-string">&apos;li&apos;</span>).length).toBe(len - <span class="hljs-number">1</span>);
   });
});

</code></pre>
<h2 id="t6&#x53C2;&#x8003;">&#x53C2;&#x8003; <a href="#t6&#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="https://github.com/facebook/create-react-app">create-react-app</a></li>
<li><a href="https://facebook.github.io/jest/">jest</a></li>
<li><a href="https://facebook.github.io/jest/docs/en/expect.html">expect</a></li>
<li><a href="https://facebook.github.io/jest/zh-Hans">jest&#x4E2D;&#x6587;&#x7F51;</a></li>
<li><a href="http://airbnb.io/enzyme/">enzyme</a></li>
<li><a href="http://jasmine.github.io/">Jasmine</a></li>
<li><a href="https://github.com/gotwarlost/istanbul">istanbul</a></li>
<li><a href="https://www.npmjs.com/package/prop-types">prop-types</a></li>
<li><a href="https://gitee.com/zhufengpeixun/zf-react-test">zf-react-test</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
